package de.hsmannheim.poolraumapp.fragment;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import de.hsmannheim.poolraumapp.R;
import de.hsmannheim.poolraumapp.activity.DetailActivity;
import de.hsmannheim.poolraumapp.activity.NavigationActivity;
import de.hsmannheim.poolraumapp.db.RoomSearcher;
import de.hsmannheim.poolraumapp.db.RoomSearcherListener;
import de.hsmannheim.poolraumapp.fragment.NavigationFragment.NavigationListener;
import de.hsmannheim.poolraumapp.model.Result;
import de.hsmannheim.poolraumapp.model.RoomReservation;

/**
 * A fragment representing a single Item detail screen. This fragment is either
 * contained in a {@link NavigationActivity} in two-pane mode (on tablets) or a
 * {@link DetailActivity} on handsets.
 */
public class ResultsFragment extends PoolraumFragment implements RoomSearcherListener {
	private static final String TAG = ResultsFragment.class.getName();
	private ProgressDialog progressDialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_results, container, false);
		
		showResults(view);
		
		return view;
	}
	
	private void showResults(View view) {
		List<Result> results = getRoomSearcher().search(getRoomQuery());
		ListView list = (ListView)view.findViewById(R.id.results_list);
		list.setAdapter(new ResultAdapter(getActivity(), R.layout.fragment_results_item, results));
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
		RoomSearcher roomSearcher = getRoomSearcher();
		
		synchronized (roomSearcher) {			
			roomSearcher.setRoomSearcherListener(this);
			if (roomSearcher.isUpdating()) {
				  progressDialog = new ProgressDialog(activity);
				  progressDialog.setMessage("Loading...");
				  progressDialog.setCancelable(true);
				  progressDialog.show();
			}
		}
	}
	
	@Override
	public void onDetach() {
		super.onDetach();
		RoomSearcher roomSearcher = getRoomSearcher();
		
		synchronized (roomSearcher) {			
			roomSearcher.setRoomSearcherListener(null);
			if (progressDialog != null) {
				progressDialog.hide();
				progressDialog = null;
			}
		}
	}
	
	private class ResultAdapter extends ArrayAdapter<Result> {
		private LayoutInflater inflater;
		private int layout;
		
		public ResultAdapter(Context context, int layout, List<Result> results) {			
			super(context, layout, results);
			
			this.inflater = (LayoutInflater)context.getSystemService(
						Context.LAYOUT_INFLATER_SERVICE);
			this.layout = layout;
		}
	
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Result result = getItem(position);
			
			View item = inflater.inflate(layout, null);
			
			TextView room = (TextView)item.findViewById(R.id.results_item_room);
			room.setText(result.getBuilding() + result.getRoomNumber());
			
			TextView time = (TextView)item.findViewById(R.id.results_item_time);
			time.setText(result.getBegin().toString("HH:mm:ss") + " - " + result.getEnd().toString("HH:mm:ss"));
			
			CheckBox linux = (CheckBox)item.findViewById(R.id.results_item_linux);
			linux.setChecked(result.hasLinux());
			linux.setEnabled(false);
			
			CheckBox mac = (CheckBox)item.findViewById(R.id.results_item_mac);
			mac.setChecked(result.hasMac());
			mac.setEnabled(false);
			
			CheckBox windows = (CheckBox)item.findViewById(R.id.results_item_windows);
			windows.setChecked(result.hasWindows());
			windows.setEnabled(false);
			
			return item;
		}
	}

	@Override
	public void updatingStatusChanged(boolean updating) {
		if (updating == false) {
			Handler mainHandler = new Handler(getActivity().getMainLooper());
			mainHandler.post(new Runnable() {
				@Override
				public void run() {
					showResults(getView());
					if (progressDialog != null) {
						progressDialog.hide();
						progressDialog = null;
					}
				}
			});
		}
	}
}
